import request from '@/common/request.js'
import { getUUID } from '@/common/utils.js'

export default {
  namespaced: true,
  state: {
    token: uni.getStorageSync('access_token') || '', // 系统后端签发token
    userInfo: uni.getStorageSync('userInfo') || {}, // 系统返回用户信息
    // deviceId: getUUID(), // 用户设备ID（通过工具函数生成的UUID），在用户使用一次小程序过程中，多个接口共用此id
    personalInfo: uni.getStorageSync('personalInfo') || {},  //用户个人资料
  },
  mutations: {
    // 保存token
    setToken: (state, data) => {
      state.token = data
      uni.setStorageSync('access_token', data)
    },
    // 清除token
    clearToken: (state) => {
      state.token = ''
      uni.removeStorageSync('access_token')
    },
    // 保存用户信息
    setUserInfo: (state, data) => {
      state.userInfo = data
      uni.setStorageSync('userInfo', data)
    },
    // 设置用户个人基本资料
    setPeronalInfo: (state, data) => {
      state.personalInfo = data
      uni.setStorageSync('personalInfo', data)
    },
  },
  actions: {
    // 微信登录
    async loginForWexin({ commit }, data) {
      const { datas: loginInfo } = await request({
        url: '/api-uaa/oauth/wxMa/token',
        data,
        header: {
          Authorization: 'Basic ZHJpdmVyTWluaWFwcDoxMTExMTE='
        }
      })
      commit('setToken', loginInfo.access_token)
      return loginInfo
    },
    
    // 密码登录
    async loginForPassword({ commit }, data) {
      const { datas: loginInfo } = await request({
        url: '/api-uaa/app/oauth/user/token',
        data,
        header: {
          Authorization: 'Basic ZHJpdmVyTWluaWFwcDoxMTExMTE='
        }
      })
      commit('setToken', loginInfo.access_token)
      return loginInfo
    },
    
    // 手机短信登录
    async loginForPhone({ commit }, data) {
      const { datas: loginInfo } = await request({
        // url: '/api-uaa/app/oauth/sms/token',
        url: '/api-uaa/wxMa/oauth/sms/token',
        data,
        header: {
          Authorization: 'Basic ZHJpdmVyTWluaWFwcDoxMTExMTE='
        }
      })
      commit('setToken', loginInfo.access_token)
      return loginInfo
    },
	//手机号加密码加验证码登录
    async loginForPhoneAndPwd({ commit }, data) {
      const { datas: loginInfo } = await request({
        url: '/api-uaa/wxMa/oauth/sms/pwd/token',
        data,
        header: {
          Authorization: 'Basic ZHJpdmVyTWluaWFwcDoxMTExMTE='
        }
      })
      commit('setToken', loginInfo.access_token)
      return loginInfo
    },
    // 获取用户信息
    async getUserInfoFn({ commit }) {
      const { datas: userInfo } = await request({
        url: '/api-user/app/users/current',
        method: 'GET'
      })
      commit('setUserInfo', userInfo)
      commit('setPeronalInfo',userInfo)
      return userInfo
    },
    
    // 刷新用户信息
    refreshUserInfo({ state, dispatch }) {
      if (state.userInfo.id) {
        dispatch('getUserInfoFn')
      }
    },
    
    // 清除用户信息
    clearUserInfo({ commit }) {
      commit('setToken', '')
      commit('setUserInfo', {})
      commit('setPeronalInfo', {})
    },
  },
	getters: {
		getUserInfo(state){
			return state.userInfo
		},
    getPeronalInfo(state) {
      return state.personalInfo
    }
	}
}